package com.tanhua.manage.listener;

import com.tanhua.commons.templates.HuaWeiUGCTemplate;
import com.tanhua.domain.mg.Publish;
import com.tanhua.dubbo.api.PublishApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Array;
import java.util.List;


@Component
@RocketMQMessageListener(topic = "ysx_dynamicReview",consumerGroup = "dynamicReview_group")
@Slf4j
public class DynamicViewListener implements RocketMQListener<String> {
    //订阅publishApi
    @Reference
    private PublishApi publishApi;
    //注入HuaWeiTemplate
    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    /**
     * 根据发布id查询状态并审核
     * @param message
     */
    @Override
    public void onMessage(String message) {
      /*  //将publishId转换成ObjectId类型
        ObjectId publishId = new ObjectId(message);*/
        //根据发布id查询动态的状态信息
        Publish publish = publishApi.findPublishById(message);
        //判断状态是否为0 如果为0那么就进行审核
        if(!ObjectUtils.isEmpty(publish)){
            if(publish.getState()==0){
                //默认状态为已驳回
                Integer state=2;
                //获取文字信息
                String textContent = publish.getTextContent();
                //获取图片信息
                List<String> medias = publish.getMedias();
                //伪调用华为云api
               /* boolean textContentCheck = huaWeiUGCTemplate.textContentCheck(textContent);
                boolean imageContentCheck = huaWeiUGCTemplate.imageContentCheck(medias.toArray(new String[]{}));*/
               boolean textContentCheck=true;
               boolean imageContentCheck=true;
               //判断是否审核通过
                if(textContentCheck&&imageContentCheck){
                   state=1;
                }
                //调用publishApi方法 传入发布id和状态信息
                publishApi.updateStateById(message,state);

            }
        }


    }
}
